/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.saving;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.gl.Tax;
import src.domain.saving.Saving;
import src.domain.security.Users;
import src.services.gl.TaxModel;
import src.services.saving.SavingModel;
import src.util.Messager;
import src.view.security.LoginBean;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class SavclosureBean implements Serializable {

    private Saving savclosure;  
    private List<SelectItem> availableBank;  
    private boolean editMode = false;
    private String defaultButton="doNothingSavclose";
    private boolean showLookup=false;
    private Users user;
       
    public SavclosureBean() {
        ambilUser();
        newData();
    }

    public boolean isShowLookup() {
        return showLookup;
    }

    public void setShowLookup(boolean showLookup) {
        this.showLookup = showLookup;
    }
        
    public boolean isEditMode() {
        return editMode;
    }
    
    public Saving getSavclosure() {
        return savclosure;
    }

    public void setSavclosure(Saving savclosure) {
        this.savclosure = savclosure;
    }

    public List<SelectItem> getAvailableBank() {
        return availableBank;
    }

    public void setAvailableBank(List<SelectItem> availableBank) {
        this.availableBank = availableBank;
    }
        
    public Double getInterestTaxName(Integer id) {  
        TaxModel tm = new TaxModel();
        Double txRate = null;
        Tax tax = tm.getById(id);
        
        if (tax != null) txRate = tax.getTaxRate().doubleValue();
                    
        return txRate;
    }
    
    public void getSavingSrc(String accntNo) {
        if ("null".equals(accntNo)) accntNo = savclosure.getAccountNo();
        if (accntNo!=null ) {
            SavingModel helper = new SavingModel();
            Saving saving = helper.getByAccount(accntNo);
                        
            if (saving==null) {
                Messager.showWarning("accntNotFound");
                this.savclosure = new Saving();
                
            } else {
                this.savclosure = saving;
                this.savclosure.setCloseDate(Calendar.getInstance().getTime());
                //submitSavclose
                this.defaultButton="doNothingSavclose";
            }
        }
       // RequestContext.getCurrentInstance().addCallbackParam("success", found); 
    }
    
    public void search() {}
        
    private void initForm() {
        this.editMode = true;
        this.defaultButton="doNothingSavclose";
        showLookup=false;
    }
        
    public final void newData() {  
        initForm();
        if (this.savclosure!=null) savclosure = null;
        
        this.savclosure = new Saving();
        this.savclosure.setCloseDate(Calendar.getInstance().getTime());
        FacesContext ftx = FacesContext.getCurrentInstance();
        ftx.renderResponse();
    }
    
    public void openData() {
        initForm();
    }
        
    public boolean validate() {
        return true;
    }
    
    public void save() {
        RequestContext context = RequestContext.getCurrentInstance();
        SavingModel helper = new SavingModel();
                
        savclosure.setClosedBy(Integer.toString(user.getUserId()));
        boolean success = helper.updateClosureDate(savclosure);
                
        this.editMode = !success;
        Messager.showSaveUpdateStatus(success);     
        context.addCallbackParam("success", success);  
    }
    
    public void trySave() {
        if (this.validate()) this.save();
    }

    public String getDefaultButton() {
        return defaultButton;
    }

    public void setDefaultButton(String defaultButton) {
        this.defaultButton = defaultButton;
    }

    public void handleCloseLookup(CloseEvent event) {
        this.showLookup=false;
    }
    
    private void ambilUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean logBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        this.user = logBean.getSelectedUser();
    }
    
    
}
